package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
 * 乱码
 * 拿到前端的值
 * 封装到用户实体类model
 * 调用服务逻辑层
 * 得到结果
 * 返回数据
 */
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String birthday = req.getParameter("birthday");
        String height = req.getParameter("height");
        String email = req.getParameter("email");
        String[] hobby = req.getParameterValues("hobby");

        UserModel userModel = new UserModel();
        userModel.setPassword(password);
        userModel.setUsername(username);
        userModel.setCode(code);
        userModel.setSex(sex);
        userModel.setPhone(phone);
        userModel.setBirthday(birthday);
        int hei = 0;
        try {
            hei = Integer.parseInt(height);
        } catch (Exception e) {
        }
        userModel.setHeight(hei);
        userModel.setEmail(email);
        userModel.setHobby(Arrays.toString(hobby));


        IUserService iUserService = new UserServiceImpl();
        String result = iUserService.add(userModel);

//        req.setAttribute("result", result);
//        req.getRequestDispatcher("/html/register.html").forward(req, resp);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);

    }
    /**
     * 1.编写注册的页面
     * 2.判断是否输入正确   正则表达式     form表单  required   action 路径， method 方法
     * 3.更具action的路径找到servlet的注解
     *    乱码
     *    拿到前端的值
     *    封装到用户实体类model
     *   调用服务逻辑层
     *    得到结果
     *    返回数据
     * 4.service服务逻辑层
     *    调用数据层 dao
     * 5.dao  数据实现层
     *     封装sql语句
     *    调用jdbc
     *    封装结果集
     *    返回结果集
     *
     *
     * 1.js form表单的封装
     * 2. js设置样式
     * 3.js 的简单封装   只要达到2个以上相同的代码都要封装
     * 4.注意return的用法
     * 5.radio  和checkbox的取值，赋值
     * 6.数据实现层查询语句封装 注意 类为null和类的属性值为null的区别
     *
     */
}
